<template>
  <div id="register">
    <el-form
      class="register-container"
      label-position="left"
      label-width="0px"
      :model="registerUser"
    >
      <h3 class="register-title" style="text-align: center">用户注册</h3>
      <el-form-item label="">
        <el-input
          type="text"
          v-model="registerUser.username"
          auto-complete="off"
          placeholder="用户名"
          clearable
          maxlength="20"
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          v-model="registerUser.password"
          auto-complete="off"
          placeholder="密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input
          v-model="registerUser.confirmedPassword"
          auto-complete="off"
          placeholder="确认密码"
          show-password
        >
        </el-input>
      </el-form-item>
      <el-form-item class="button-container" style="width: 100%">
        <el-button-group>
          <el-button
            type="primary"
            style="width: 30%; background: #505458; border: none"
            icon="el-icon-edit"
            @click="toLogin"
          >
            登录
          </el-button>
          <el-button
            type="primary"
            style="width: 30%; background: #505458; border: none"
            icon="el-icon-share"
            @click="register"
            >提交</el-button
          >
          <el-button
            type="primary"
            style="width: 30%; background: #505458; border: none"
            icon="el-icon-delete"
            @click="clearForm"
          >
            清空
          </el-button>
        </el-button-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "RegisterView",
  data() {
    return {
      // 注册用户信息
      registerUser: {
        username: "",
        password: "",
        confirmedPassword: "",
      },
    };
  },
  mixins: ["toLogin"],
  methods: {
    // 清空注册用户信息
    clearForm() {
      this.registerUser = {
        username: "",
        password: "",
        confirmedPassword: "",
      };
    },
    // 注册函数
    register() {
      let outer = this;
      this.$store.dispatch("register", {
        username: this.registerUser.username,
        password: this.registerUser.password,
        confirmedPassword: this.registerUser.confirmedPassword,
        success(resp) {
          if (resp.data.code == 200) {
            outer.$message({
              message: "注册成功，快去登陆吧！",
              type: "success",
            });
            outer.clearForm();
            outer.$router.push("/user/login");
          } else {
            outer.$message.error(resp.data.message);
          }
        },
        error(resp) {
          console.log(resp);
        },
      });
    },
  },
};
</script>

<style scoped>
#register {
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  /* position: fixed; */
  margin: 0px;
  padding: 0px;
}
.register-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 90px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaee;
  box-shadow: 0 0 25px #cac6c6;
}
.register-title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.button-container {
  display: flex;
  justify-content: space-between;
}
</style>
